package sqltest;



import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.PreparedStatement;

public class PreparedStatementExercise01 {
    public static void main(String[] args) {
        PreparedStatement ps = null;
        Connection conn = null;

        try {
            //        1.注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //        2.获取连接
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
            //        3.获取预编译的数据库操作对象（？是占位符）
            String sql = "insert into table1(id,name) values(?,?)";
            ps = conn.prepareStatement(sql);
            ps.setInt(1, 60);
            ps.setString(2, "dad");
            /*删除语句
            String sql = "delete from table1 where  id=?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1, 60);
             */
            //      4.执行sql语句
            int count = ps.executeUpdate();
//            int count2 = ps.executeUpdate("drop table admin");
            int count3 = ps.executeUpdate("create table if not exists admin(id int(20) primary key ," + "name varchar (20) not null);");
            System.out.println(count == 1 ? "保存成功" : "保存失败");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //      6.释放资源
            if (ps!=null){
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
